import Request from 'luch-request'
import baseConfig from '@/config/index'
const http = new Request({
	baseURL: `${baseConfig.baseUrl}`,
	dataType: 'json',
	timeout: 30000
})
http.interceptors.request.use(
	(config) => {
		if (config.url != '/api.php/landing/landing') {
			config.header = {
				Authorization: uni.getStorageSync('token') || ''
			}
		}
		return config
	},
	(config) => {
		return Promise.reject(config)
	}
)

// 响应拦截器
http.interceptors.response.use(
	(response) => {
		const res = response
		// 响应成功
		switch (res.code || res.statusCode) {
			case 200:
				if (res.data.code != 200) {
					uni.showToast({
						title: res.data.msg,
						icon: 'none'
					})
				}
				return res.data.data ? res.data.data : res
			default:
				uni.showToast({
					title: res.message,
					icon: 'none'
				})
				return Promise.reject(res)
		}
	},
	(response) => {
		// 响应失败拦截器
		// console.log(response, '失败')
		const res = response
		switch (res.code || res.statusCode) {
			case 500:
				uni.showToast({
					title: '服务器响应错误',
					icon: 'none'
				})
				return Promise.reject(response)
			default:
				//这里关闭所有页面 跳转到登录 因为鉴权失败需要重新登录 为了测试先不写跳转
				return Promise.reject(response)
		}
	}
)
export default http
